const { merge } = require("webpack-merge");
const commonConfig = require('./webpack.common');
const { DefinePlugin, LoaderOptionsPlugin } = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserWebpackPlugin = require("terser-webpack-plugin");
const OptionCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')
const path = require("path");
module.exports = merge(commonConfig, {
  mode: "production",
  output: {
    filename: '[name]-[hash:8].bundle.js',
    chunkFilename: '[name]-[hash:8].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    clean: true,
  },
  module: {
    rules: [
      {
        test: /\.less$/,
        use: [MiniCssExtractPlugin.loader, "css-loader", "less-loader"]
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, "css-loader"]
      }
    ]
  },
  plugins: [
    new DefinePlugin({
      BASE_URL: process.env.NODE_ENV
    }),
    new CleanWebpackPlugin(),
    new MiniCssExtractPlugin()
  ],
  optimization: {
    minimizer: [
      new OptionCssAssetsWebpackPlugin({
        cssProcessorOptions: {
          discardComments: { remove: true } //移除注释
        }
      }),
      new TerserWebpackPlugin({
        parallel: true, //开启多线程来提高构建速度
        terserOptions: {
          warnings: false, //不展示warning
          compress: {
            unused: true, //去除未使用的
            drop_debugger: true, //移除debugger
            drop_console: true //去除console
          },
          output: {
            comments: false //去除注释
          }
        }
      })
    ]
  }
});
